﻿using System.Web.UI;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Summary description for ImageButton
	/// </summary>
	public class ImageButton : BaseCommandControl, IImageControl
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public ImageButton()
		{
			//Define<string>("Src", new PropertyDefinitionOptions<string>
			//{
			//  Serializer = s => this.ResolveUrl(s),
			//});
			//Define<string>("Alt");
			//
			// TODO: Add constructor logic here
			//
			Tag = HtmlTextWriterTag.Input;
		}
		#endregion
		#region PROPS
		#region DEF
		/// <summary>
		/// Props.
		/// </summary>
		protected static new Properties _clientProperties = Define(BaseCommandControl._clientProperties, ImageControlCommon.Properties);
		/// <summary>
		/// Props.
		/// </summary>
		public override Properties ClientProperties
		{
			get
			{
				return _clientProperties;
			}
		}
		#endregion
		/// <summary>
		/// The URL of the image to be displayed.
		/// </summary>
		public string Src { get { return this.GetProperty<string>("Src"); } set { this.ViewState["Src"] = value; } }
		/// <summary>
		/// The alternate text to be display when images not supported or disabled.
		/// </summary>
		public string Alt { get { return this.GetProperty<string>("Alt"); } set { this.ViewState["Alt"] = value; } }
		public override object GenericValue
		{
			get
			{
				return this.Src;
			}
			set
			{
				this.Src = (string)value;
			}
		}
		protected internal override string SerializedValue
		{
			get
			{
				return this.Src;
			}
			set
			{
				this.Src = value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region METHODS
		#region INIT
		/// <summary>
		/// Registers ImageButton.js.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.InputControl.js", typeof(BaseCommandControl)));
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.ImageButton.js", typeof(BaseCommandControl)));
		}
		#endregion
		#region RENDER
		/// <summary>
		/// Adds the type="image", src and alt attributes.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			writer.AddAttribute(HtmlTextWriterAttribute.Type, "image");
			writer.AddAttribute(HtmlTextWriterAttribute.Src, Image.ResolveSrc(Src));
			if (!string.IsNullOrEmpty(Alt))
				writer.AddAttribute(HtmlTextWriterAttribute.Alt, Alt);

			base.AddAttributesToRender(writer);
		}
		#endregion
		#endregion
		#endregion
	}
}